package net.anzhuo.controller.db;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import net.anzhuo.common.utils.CompanyNameUtils;
import net.anzhuo.common.utils.HttpUtils;
import net.anzhuo.entity.GsAssetsliab;
import net.anzhuo.entity.GsBase;
import net.anzhuo.entity.GsBranch;
import net.anzhuo.entity.GsBusinessProfits;
import net.anzhuo.entity.GsCashFlow;
import net.anzhuo.entity.GsCertificates;
import net.anzhuo.entity.GsChange;
import net.anzhuo.entity.GsContactInfo;
import net.anzhuo.entity.GsContracts;
import net.anzhuo.entity.GsCorpothersinfo;
import net.anzhuo.entity.GsEmployees;
import net.anzhuo.entity.GsEnterprise;
import net.anzhuo.entity.GsGaoGuan;
import net.anzhuo.entity.GsGudong;
import net.anzhuo.entity.GsHonors;
import net.anzhuo.entity.GsMember;
import net.anzhuo.entity.GsOuttouzi;
import net.anzhuo.entity.GsReportLog;
import net.anzhuo.entity.GsZiZhi;
import net.anzhuo.entity.syncdata.Branch;
import net.anzhuo.entity.syncdata.EnterpriseBase;
import net.anzhuo.entity.syncdata.EnterpriseDataInfo;
import net.anzhuo.entity.syncdata.ForeignInvest;
import net.anzhuo.entity.syncdata.Members;
import net.anzhuo.entity.syncdata.ShareHolders;
import net.anzhuo.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 *  qichacha 数据上传接口
 */
@RestController
@Slf4j
@RequestMapping("/reportUpload/")
public class EnterpriseInfoUploadController {

    @Autowired
    private GsBaseService gsBaseService;
    
    @Autowired
    private GsMemberService gsMemberService;
    
    @Autowired
    private GsChangeService gsChangeService;
    @Autowired
    private GsBranchService gsBranchService;
    @Autowired
    private GsGudongService gsGudongService;
    
    @Autowired
    private GsContactInfoService gsContactInfoService;
    @Autowired
    private GsCorpothersinfoService gsCorpothersinfoService;
    @Autowired
    private GsZiZhiService gsZiZhiService;
    @Autowired
    private GsReportLogService gsReportLogService;
    @Autowired
    private GsBusinessProfitsService gsBusinessProfitsService;
    @Autowired
    private GsCashFlowService gsCashFlowService;
    @Autowired
    private GsAssetsliabService gsAssetsliabService;
    @Autowired
    private GsGaoGuanService gsGaoGuanService;
    @Autowired
    private GsContractsService gsContractsService;
    @Autowired
    private GsEmployeesService gsEmployeesService;
    @Autowired
    private GsHonorsService gsHonorsService;
    @Autowired
    private GsCertificatesService gsCertificatesService;
    
    @Autowired
    private GsOuttouziService gsOuttouziService;
    
    @Autowired
    private GsEnterpriseService gsEnterpriseService;

    private  Integer updateIndex = 0;
    private Integer insertIndex  = 0 ;


    private String dealBigDecimal(BigDecimal value) {
        DecimalFormat df2 = new DecimalFormat("###0.00");
        if (value == null) {
            return "—";
        } else {
            return df2.format(value);
        }
    }


    @RequestMapping(value="EnterpriseInfoUpload",method = RequestMethod.POST)
    @ResponseBody
    public boolean insert(@RequestBody EnterpriseDataInfo enterpriseDataInfo) throws Exception {
      
    	log.info("EnterpriseInfoUpload:param:"+JSON.toJSONString(enterpriseDataInfo));
    	// Base
    	GsBase base=null;
    	String md5="";
    	boolean isExists=false;
    	List<GsBase> listGsBase=gsBaseService.getByNameList(CompanyNameUtils.getEnglishBraces(enterpriseDataInfo.getEnterpriseBase().getName()));
    	if(listGsBase.size()==0)
    	{
    		listGsBase=gsBaseService.getByNameList(CompanyNameUtils.getChineseBraces(enterpriseDataInfo.getEnterpriseBase().getName()));
    		if(listGsBase.size()==0)
    		{
    			listGsBase=gsBaseService.getByCreditCodeList(enterpriseDataInfo.getEnterpriseBase().getCreditCode());
    			if(listGsBase.size()>0)
    			{
    				isExists=true;
    				base=listGsBase.get(0);
    			}
    			
    		}else
    		{
    			isExists=true;
				base=listGsBase.get(0);
    		}
    	}else
    	{
    		isExists=true;
			base=listGsBase.get(0);
    	}
    	//判断 enterprise 表中的是否存在keyNo
    	
    	if(isExists)
    	{
    		md5=base.getMd5();
    		List<GsEnterprise> gsEnterpriseList=gsEnterpriseService.getByKeyNo(md5);
    		if(gsEnterpriseList.size()>0)
    		{
    			EnterpriseBase enterpriseBase=enterpriseDataInfo.getEnterpriseBase();
    			enterpriseBase.setKeyNo(md5);
    			enterpriseDataInfo.setEnterpriseBase(enterpriseBase);
    		}
    		base.setName(enterpriseDataInfo.getEnterpriseBase().getName());
    		base.setName2(enterpriseDataInfo.getEnterpriseBase().getOldNames());
    		base.setMd5(enterpriseDataInfo.getEnterpriseBase().getKeyNo());
    		base.setRegTime(enterpriseDataInfo.getEnterpriseBase().getRegTime());
    		base.setEnterpriseStatus(enterpriseDataInfo.getEnterpriseBase().getRegStatus());
    		base.setRegAddress(enterpriseDataInfo.getEnterpriseBase().getRegAddress());
    		base.setCreditCode(enterpriseDataInfo.getEnterpriseBase().getCreditCode());
    		base.setTaxNumber(enterpriseDataInfo.getEnterpriseBase().getTaxCode());
    		base.setDeptCode(enterpriseDataInfo.getEnterpriseBase().getOrgCode());
    		base.setOperScope(enterpriseDataInfo.getEnterpriseBase().getRegScope());
    		base.setRegZb(enterpriseDataInfo.getEnterpriseBase().getRegCapital());
    		base.setWebSite(enterpriseDataInfo.getEnterpriseBase().getWebSite());
    		base.setEmail(enterpriseDataInfo.getEnterpriseBase().getEmail());
    		base.setLegal(enterpriseDataInfo.getEnterpriseBase().getLegal());
    		base.setEnterpriseType(enterpriseDataInfo.getEnterpriseBase().getRegType());
    		base.setIndustry(enterpriseDataInfo.getEnterpriseBase().getIndustryType());
    		base.setApprovalTime(enterpriseDataInfo.getEnterpriseBase().getApprovalTime());
    		base.setRegAuth(enterpriseDataInfo.getEnterpriseBase().getRegOrg());
    		base.setProvince(enterpriseDataInfo.getEnterpriseBase().getProvince());
    		base.setEnglishName(enterpriseDataInfo.getEnterpriseBase().getEnglishName());
    		base.setEmployeesCount(enterpriseDataInfo.getEnterpriseBase().getEmployeesCount());
    		base.setInsuredCount(enterpriseDataInfo.getEnterpriseBase().getInsuredCount());
    		base.setIndustry(enterpriseDataInfo.getEnterpriseBase().getIndustryType());
    		base.setUpdateTime(enterpriseDataInfo.getEnterpriseBase().getUpdateTime());
    		base.setRegNumber(enterpriseDataInfo.getEnterpriseBase().getRegNum());
    		base.setYear(enterpriseDataInfo.getEnterpriseBase().getRegTime().substring(0, 4));
    		gsBaseService.update(base);
    		
    	}else
    	{
    		base.setName(enterpriseDataInfo.getEnterpriseBase().getName());
    		base.setName2(enterpriseDataInfo.getEnterpriseBase().getOldNames());
    		base.setMd5(enterpriseDataInfo.getEnterpriseBase().getKeyNo());
    		base.setRegTime(enterpriseDataInfo.getEnterpriseBase().getRegTime());
    		base.setEnterpriseStatus(enterpriseDataInfo.getEnterpriseBase().getRegStatus());
    		base.setRegAddress(enterpriseDataInfo.getEnterpriseBase().getRegAddress());
    		base.setCreditCode(enterpriseDataInfo.getEnterpriseBase().getCreditCode());
    		base.setTaxNumber(enterpriseDataInfo.getEnterpriseBase().getTaxCode());
    		base.setDeptCode(enterpriseDataInfo.getEnterpriseBase().getOrgCode());
    		base.setOperScope(enterpriseDataInfo.getEnterpriseBase().getRegScope());
    		base.setRegZb(enterpriseDataInfo.getEnterpriseBase().getRegCapital());
    		base.setWebSite(enterpriseDataInfo.getEnterpriseBase().getWebSite());
    		base.setEmail(enterpriseDataInfo.getEnterpriseBase().getEmail());
    		base.setLegal(enterpriseDataInfo.getEnterpriseBase().getLegal());
    		base.setEnterpriseType(enterpriseDataInfo.getEnterpriseBase().getRegType());
    		base.setIndustry(enterpriseDataInfo.getEnterpriseBase().getIndustryType());
    		base.setApprovalTime(enterpriseDataInfo.getEnterpriseBase().getApprovalTime());
    		base.setRegAuth(enterpriseDataInfo.getEnterpriseBase().getRegOrg());
    		base.setProvince(enterpriseDataInfo.getEnterpriseBase().getProvince());
    		base.setEnglishName(enterpriseDataInfo.getEnterpriseBase().getEnglishName());
    		base.setEmployeesCount(enterpriseDataInfo.getEnterpriseBase().getEmployeesCount());
    		base.setInsuredCount(enterpriseDataInfo.getEnterpriseBase().getInsuredCount());
    		base.setIndustry(enterpriseDataInfo.getEnterpriseBase().getIndustryType());
    		base.setUpdateTime(enterpriseDataInfo.getEnterpriseBase().getUpdateTime());
    		base.setRegNumber(enterpriseDataInfo.getEnterpriseBase().getRegNum());
    		base.setYear(enterpriseDataInfo.getEnterpriseBase().getRegTime().substring(0, 4));
    		gsBaseService.save(base);
    	}
    	//shareHolders
    	List<GsGudong> listGsGudong=gsGudongService.getByMd5(md5);
    	GsGudong gsGudong=new GsGudong();
    	if(listGsGudong.size()>0)
    	{
    		gsGudong.setCoMd5(md5);
    		gsGudongService.delete(gsGudong);
    	}
    	ShareHolders sh=new ShareHolders();
    	for(int i=0;i<enterpriseDataInfo.getShareHolder().size();i++)
    	{
    		gsGudong=new GsGudong();
    		sh=enterpriseDataInfo.getShareHolder().get(i);
    		gsGudong.setShareholder(sh.getShareHolder());
    		gsGudong.setCapitalPercent(sh.getShareHoldingRatio());
    		gsGudong.setCapitaltime(sh.getHoldCapiTime());
    		gsGudong.setCoMd5(sh.getCoKeyNo());
    		gsGudong.setConfisCapital(sh.getHoldCapi());
    		List<GsBase> gsBaseList=gsBaseService.getByNameList(sh.getShareHolder());
    		if(gsBaseList.size()>0)
    		{
    			GsBase gsBase=gsBaseList.get(0);
    			
    		 List<GsEnterprise>	gsEnterpriseList= gsEnterpriseService.getByKeyNo(gsBase.getMd5());
    		 if(gsEnterpriseList.size()>0)
    		 {
    			 sh.setKeyNo(gsBase.getMd5());
    		 }
    		}
    		gsGudong.setMd5(sh.getKeyNo());
    		if(sh.getShareHolder().contains("公司"))
    		{
    			gsGudong.setCapitaltype("1");
    			gsGudong.setStockType("1");
    		}else if(sh.getShareHolder().trim().length()<=4)
    		{
    			gsGudong.setCapitaltype("0");
    			gsGudong.setStockType("0");
    		}else
    		{
    			gsGudong.setCapitaltype("2");
    			gsGudong.setStockType("2");
    		}
    		gsGudong.setUpdateTime(sh.getUpdateTime());
    		gsGudongService.save(gsGudong);
    	}
    	//member   	
    	List<GsMember> listGsMember=gsMemberService.getByMd5(md5);
    	GsMember gsMember=new GsMember();
    	if(listGsMember.size()>0)
    	{
    		gsMember.setCoMd5(md5);
    		gsMemberService.delete(gsMember);
    	}  	
    	Members m=new Members();
    	for(int i=0;i<enterpriseDataInfo.getMember().size();i++)
    	{
    		m=new Members();
    		m=enterpriseDataInfo.getMember().get(i);
    		gsMember.setMd5(m.getKeyNo());
    		gsMember.setName(m.getName());
    		gsMember.setPosition(m.getPosition());
    		gsMember.setCoMd5(m.getCoKeyNo());
    		gsMember.setUpdateTime(m.getUpdateTime());
    		gsMemberService.save(gsMember);		
    	}
    	//Branch
    	GsBranch gsBranch=new GsBranch();
    	List<GsBranch> listGsBranch=gsBranchService.getByMd5(md5);
    	if(listGsBranch.size()>0)
    	{
    		gsBranch.setCoMd5(md5);
    		gsBranchService.delete(gsBranch);
    	}  	
    	Branch branch=new Branch();
    	for(int i=0;i<enterpriseDataInfo.getBranch().size();i++)
    	{
    		branch=new Branch();
    		gsBranch=new GsBranch();
    		branch=enterpriseDataInfo.getBranch().get(i);
    		List<GsBase> gsBaseList=gsBaseService.getByNameList(branch.getName());
    		if(gsBaseList.size()>0)
    		{
    			GsBase gsBase=gsBaseList.get(0);
    			
    		 List<GsEnterprise>	gsEnterpriseList= gsEnterpriseService.getByKeyNo(gsBase.getMd5());
    		 if(gsEnterpriseList.size()>0)
    		 {
    			 branch.setKeyNo(gsBase.getMd5());
    		 }
    		}
    		gsBranch.setMd5(branch.getKeyNo());
    		gsBranch.setEnterpriseName(branch.getName());
    		gsBranch.setCoMd5(m.getCoKeyNo());
    		gsBranch.setUpdateTime(m.getUpdateTime());
    		gsBranchService.save(gsBranch);		
    	}
       //ForeignInvest
       GsOuttouzi gsOuttouzi=new GsOuttouzi();
       ForeignInvest foreignInvest=new ForeignInvest();
       List<GsOuttouzi> listGsOuttouzi=gsOuttouziService.getByMd5(md5);
       if(listGsOuttouzi.size()>0)
       {
    	   gsOuttouzi.setCoMd5(md5);
    	   gsOuttouziService.delete(gsOuttouzi);
       }
       for(int i=0;i<enterpriseDataInfo.getForeignInvest().size();i++)
   	   {
    	   foreignInvest=new ForeignInvest();
    	   gsOuttouzi=new GsOuttouzi();
    	   foreignInvest=enterpriseDataInfo.getForeignInvest().get(i);
    	   List<GsBase> gsBaseList=gsBaseService.getByNameList(foreignInvest.getName());
   		   if(gsBaseList.size()>0)
   		   {
   			GsBase gsBase=gsBaseList.get(0);		
   		    List<GsEnterprise>	gsEnterpriseList= gsEnterpriseService.getByKeyNo(gsBase.getMd5());
   		    if(gsEnterpriseList.size()>0)
   		    {
   		    	foreignInvest.setKeyNo(gsBase.getMd5());
   		    }
   		   }
    	   gsOuttouzi.setMd5(foreignInvest.getKeyNo());
    	   gsOuttouzi.setEnterpriseName(foreignInvest.getName());
    	   gsOuttouzi.setAmount(foreignInvest.getShouldCapi());
    	   gsOuttouzi.setInvestmentRatio(foreignInvest.getRegPercent());
    	   gsOuttouzi.setRegCapital(foreignInvest.getRegCap());
    	   gsOuttouzi.setState(foreignInvest.getState());
    	   gsOuttouzi.setRegTime(foreignInvest.getRegTime());
    	   gsOuttouzi.setCoMd5(foreignInvest.getCoKeyNo());
    	   gsOuttouzi.setUpdateTime(foreignInvest.getUpdateTime());
    	   gsOuttouziService.save(gsOuttouzi);		
   	   }
       //gs_assetsliab
       GsAssetsliab gsAssetsliab=new GsAssetsliab();
       List<GsAssetsliab> listGsAssetsliab=gsAssetsliabService.getByMd5(md5);
       for(int i=0;i<listGsAssetsliab.size();i++)
   	   {
    	   gsAssetsliab= listGsAssetsliab.get(i);
    	   gsAssetsliab.setCoMd5(md5);
    	   gsAssetsliabService.update(gsAssetsliab);
   	   }
       //gs_beian
       GsContactInfo GsContactInfo=new GsContactInfo();
       List<GsContactInfo> listGsContactInfo=gsContactInfoService.getBycoMd5(md5);
       for(int i=0;i<listGsContactInfo.size();i++)
   	   {
    	   GsContactInfo= listGsContactInfo.get(i);
    	   GsContactInfo.setCoMd5(md5);
    	   gsContactInfoService.update(GsContactInfo);
   	   }
       //
       
        return true;
    }
}
